﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Configuration;

namespace MODEL
{
    public class AppSetting
    {
        public TimeSpan ValveControlBeginDelayTime { get; set; }

        public string MySqlConnectionStr { get; set; }

        public string GetWeatherPictureHttpUrl { get; set; }

        public string UploadWeatherPictureFTPUrl { get; set; }

        public string UploadWeatherPictureFTPAccount { get; set; }

        public string UploadWeatherPictureFTPPassword { get; set; }

        public User User { get; set; } = new User();

        private AppSetting() { }

        private static object m_InstanceLock = new object();
        private static AppSetting m_Instance;
        public static AppSetting Instance {
            get {
                if (m_Instance == null)
                {
                    lock (m_InstanceLock)
                    {
                        if (m_Instance == null)
                        {
                            m_Instance = new AppSetting();
                        }
                    }
                }
                return m_Instance;
            }
        }

        public void Initialize()
        {
            try
            {
                Instance.MySqlConnectionStr = ConfigurationManager.AppSettings["MySqlConnectionStr"].ToString();
                Instance.UploadWeatherPictureFTPUrl = ConfigurationManager.AppSettings["UploadWeatherPictureFTPUrl"].ToString();
                Instance.UploadWeatherPictureFTPAccount = ConfigurationManager.AppSettings["UploadWeatherPictureFTPAccount"].ToString();
                Instance.UploadWeatherPictureFTPPassword = ConfigurationManager.AppSettings["UploadWeatherPictureFTPPassword"].ToString(); 
                Instance.GetWeatherPictureHttpUrl = ConfigurationManager.AppSettings["GetWeatherPictureHttpUrl"].ToString();
                try
                {
                    Instance.ValveControlBeginDelayTime = TimeSpan.Parse(ConfigurationManager.AppSettings["ValveControlBeginDelayTime"].ToString());
                }
                catch
                {
                    Instance.ValveControlBeginDelayTime = TimeSpan.FromMinutes(1);
                }
            }
            catch
            {
                throw new Exception("Get mysql connection string failed.");
            }
        }
    }
}
